package org.kunlong.qgen.parser;

import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Element;
import org.kunlong.qgen.domain.Choice;
import org.kunlong.qgen.domain.MultipleChoiceQuestion;
import org.kunlong.qgen.domain.QuestionHolder;
/**
 * parse the Multiple Choice Tag in XML
 * */
public class MultipleChoiceParser extends AbstractParser {
	@SuppressWarnings("unchecked")
	public void parse(Element e, QuestionHolder holder) throws Exception {
		MultipleChoiceQuestion multipleChoiceQuestion = new MultipleChoiceQuestion();
		multipleChoiceQuestion.setAnswer(getAnswer(e));//set answer
		multipleChoiceQuestion.setDescription(getDescription(e));//set description
		multipleChoiceQuestion.setQuestionType(getQuestionType());//set questiontype
		List<Element> choices = e.elements("Choice");//get all choice
		for (Element choice : choices) {
			Choice ch=new Choice();
			Attribute a = choice.attribute("correct");
			if (null != a && "true".equals(a.getText())) {
				ch.setCorrect(true);
			}
			ch.setAnswer(choice.getText().trim());
			multipleChoiceQuestion.getChoices().add(ch);
		}
		
		Element ask=e.element("Ask");
		if(ask!=null){
			//if there is ask
			String askStr=ask.getTextTrim();
			multipleChoiceQuestion.setAsk(askStr);
		}
		
		holder.add(getQuestionType(), multipleChoiceQuestion);
	}

}
